// src/pages/internship/report/apis/report.ts
import type {
  CreateOrUpdateSummaryRequest,
  ReviewSummaryRequest,
  SummaryQuery,
  SummaryReport,
  SummaryResponse,
  SummaryTask
} from "./type"
import { request } from "@/http/axios"
import * as MockApis from "./mock"

// 判断是否使用 mock 数据（开发环境使用）
const USE_MOCK = import.meta.env.DEV

/**
 * 获取学生总结任务列表
 */
export async function getStudentSummaryTasksApi(query?: {
  status?: string
}) {
  if (USE_MOCK) {
    return MockApis.getStudentSummaryTasksMock(query)
  }
  return request<ApiResponseData<{ list: SummaryTask[], total: number }>>({
    url: "/internship/student/summary-tasks",
    method: "get",
    params: query
  })
}

/**
 * 获取总结任务详情
 */
export async function getSummaryTaskDetailApi(id: number) {
  if (USE_MOCK) {
    return MockApis.getSummaryTaskDetailMock(id)
  }
  return request<ApiResponseData<SummaryTask>>({
    url: `/internship/student/summary-task/${id}`,
    method: "get"
  })
}

/**
 * 获取总结报告列表（教师审核）
 */
export async function getSummaryReportsApi(query?: SummaryQuery) {
  if (USE_MOCK) {
    return MockApis.getSummaryReportsMock(query)
  }
  return request<SummaryResponse>({
    url: "/internship/teacher/summary-reports",
    method: "get",
    params: query
  })
}

/**
 * 获取总结报告详情
 */
export async function getSummaryReportDetailApi(id: number) {
  if (USE_MOCK) {
    return MockApis.getSummaryReportDetailMock(id)
  }
  return request<ApiResponseData<SummaryReport>>({
    url: `/internship/teacher/summary-report/${id}`,
    method: "get"
  })
}

/**
 * 创建/更新总结报告
 */
export async function createOrUpdateSummaryApi(data: CreateOrUpdateSummaryRequest) {
  if (USE_MOCK) {
    return MockApis.createOrUpdateSummaryMock(data)
  }
  return request<ApiResponseData<SummaryReport>>({
    url: "/internship/student/summary-report",
    method: "post",
    data
  })
}

/**
 * 审核总结报告
 */
export async function reviewSummaryReportApi(id: number, data: ReviewSummaryRequest) {
  if (USE_MOCK) {
    return MockApis.reviewSummaryReportMock(id, data)
  }
  return request<ApiResponseData<SummaryReport>>({
    url: `/internship/teacher/summary-report/${id}/review`,
    method: "patch",
    data
  })
}

/**
 * 上传成果附件
 */
export async function uploadAchievementFileApi(file: File) {
  if (USE_MOCK) {
    return MockApis.uploadAchievementFileMock(file)
  }
  const formData = new FormData()
  formData.append("file", file)
  return request<ApiResponseData<{ url: string }>>({
    url: "/internship/student/summary-report/upload",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  })
}
